//
//  ImageCell.m
//  ReplaceImagesInAssets
//
//  Created by liboxiang on 2019/4/15.
//  Copyright © 2019 liboxiang. All rights reserved.
//

#import "ImageCell.h"

@interface ImageCell()

@property (weak) IBOutlet NSButton *checkBox;
@property (weak) IBOutlet NSImageView *igView;
@property (weak) IBOutlet NSImageView *ig2xView;
@property (weak) IBOutlet NSImageView *ig3xView;
@property (weak) IBOutlet NSTextField *nameLabel;
@property (weak) IBOutlet NSButton *igViewCheckBox;
@property (weak) IBOutlet NSButton *igView2xCheckBox;
@property (weak) IBOutlet NSButton *igView3xCheckBox;

@property (strong, nonatomic) ImageModel *imageModel;
@property (copy, nonatomic) IsSelectedStateChange stateChangeBlock;

@end

@implementation ImageCell

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}
- (IBAction)checkBoxOnClick:(id)sender {
    _imageModel.isSelected = ((NSButton *)sender).state;
    if (_stateChangeBlock) {
        _stateChangeBlock(_imageModel);
    }
}

- (IBAction)checkBox1xOnClick:(id)sender {
    if (((NSButton *)sender).state) {
        _imageModel.switchCheckBoxStatus |= SwitchCheckBoxStatus1x;
    }
    else {
        _imageModel.switchCheckBoxStatus &= ~SwitchCheckBoxStatus1x;
    }
}

- (IBAction)checkBox2xOnClick:(id)sender {
    if (((NSButton *)sender).state) {
        _imageModel.switchCheckBoxStatus |= SwitchCheckBoxStatus2x;
    }
    else {
        _imageModel.switchCheckBoxStatus &= ~SwitchCheckBoxStatus2x;
    }
}

- (IBAction)checkBox3xOnClick:(id)sender {
    if (((NSButton *)sender).state) {
        _imageModel.switchCheckBoxStatus |= SwitchCheckBoxStatus3x;
    }
    else {
        _imageModel.switchCheckBoxStatus &= ~SwitchCheckBoxStatus3x;
    }
}

- (void)refreshWithModel:(ImageModel *)model stateChange:(nonnull IsSelectedStateChange)block{
    _imageModel = model;
    _stateChangeBlock = block;
    if (model.showCheckBox) {
        _checkBox.hidden = NO;
        _checkBox.state = model.isSelected ? NSControlStateValueOn : NSControlStateValueOff;
    }
    else {
        _checkBox.hidden = YES;
    }
    switch (model.cellType) {
        case ImageCellTypeSameNameOld:
        case ImageCellTypeNotSameNameOld:
            _igViewCheckBox.hidden = YES;
            _igView2xCheckBox.hidden = YES;
            _igView3xCheckBox.hidden = YES;
        break;
        case ImageCellTypeSameNameNew:
        case ImageCellTypeNotSameNameNew:
            _igViewCheckBox.hidden = NO;
            _igView2xCheckBox.hidden = NO;
            _igView3xCheckBox.hidden = NO;
            
            _igViewCheckBox.state = model.switchCheckBoxStatus & SwitchCheckBoxStatus1x;
            _igView2xCheckBox.state = model.switchCheckBoxStatus & SwitchCheckBoxStatus2x;
            _igView3xCheckBox.state = model.switchCheckBoxStatus & SwitchCheckBoxStatus3x;
        break;
    }
    
    //图片名
    _nameLabel.stringValue = model.name;
    
    //图片处理
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:model.path];
    _igView.image = nil;
    if (image) {
        _igView.image = image;
    }
    else {
        NSLog(@"%@获取图片失败",model.path);
    }
    
    NSString *path2 = [model.path stringByReplacingCharactersInRange:[model.path rangeOfString:@"." options:NSBackwardsSearch] withString:@"@2x."];
    NSImage *image2 = [[NSImage alloc] initWithContentsOfFile:path2];
    _ig2xView.image = nil;
    if (image2) {
        _ig2xView.image = image2;
    }
    else {
        NSLog(@"%@获取图片失败",path2);
    }
    
    NSString *path3 = [model.path stringByReplacingCharactersInRange:[model.path rangeOfString:@"." options:NSBackwardsSearch] withString:@"@3x."];
    NSImage *image3 = [[NSImage alloc] initWithContentsOfFile:path3];
    _ig3xView.image = nil;
    if (image3) {
        _ig3xView.image = image3;
    }
    else {
        NSLog(@"%@获取图片失败",path3);
    }
}

@end
